$(function () {
    console.log('88888')
});

function saveNews(index){

    //获取新闻id
    var newsId = $("#newsId" + index).text();
    //获取新闻标题
    var newsTitle = $("#newsTitle" + index).val();
    //获取新闻内容
    var newsContent = $("#newsContent" + index).val();
    //获取图片id
    var uploadId = $("#imageId" + index).text();
    //新闻顺序
    var newsOrder = index+1;

    //新闻标题为空和长度验证
    if (!newsTitle) {
        ok = false;
        promptInfo("新闻标题不能为空!");
        return;
    } else {
        if (newsTitle.length > 30) {
            ok = false;
            promptInfo("新闻标题长度不能大于25个字！");
            return;
        }
    }

    //新闻内容不能为空
    if (!newsContent) {
        ok = false;
        promptInfo("新闻内容不能为空!");
        return;
    }

    var jsonData = {
        "newsId": newsId,
        "newsTitle": newsTitle,
        "newsContent": newsContent,
        "uploadId": uploadId,
        "newsOrder": newsOrder,
    };

    $.ajax({
        url: '/NewsController/updateNewInfo',
        type: 'POST',
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify(jsonData),
        success: (function (data) {
            if (data.code == "000000") {
                $.alert("保存成功！");
                setTimeout(function () {
                    var $thisTabs = parent.$('.mt-nav-bar .nav-tabs').find('li.active');
                    var navTab = parent.$multitabs._getNavTab($thisTabs);
                    //关闭当前页签
                    closeTab(navTab);
                    //打开首页
                    openTab("首页","main");
                    delStorageAndRemove(navTab);
                }, 3000)

            } else {
                alert("保存失败");
            }
        }),
        error: (function (res) {
            alert("保存失败");
        })
    });
}

function getNewsPicUpload(ele,index) {
    //文件选择
    var formData = new FormData();
    //获取选择文件个数
    var files = document.getElementById("files" + index).files;
    console.log(files);
    if (files.length == 0) {
    } else {
        //选择文件，上传选择文件
        for (let i = 0; i < files.length; i++) {
            formData.append('files', $(ele)[0].files[i]);
        }
        $.ajax({
            url: '/file/upload',
            type: 'POST',
            dataType: "json",
            contentType: false,
            data: formData,
            processData: false,
            success: (function (data) {
                if (data.code == "000000") {
                    //进行照片回显
                    var files = new Array();
                    files = data.data;
                    $("#newsPic"+index).attr('src',files[0].accessUrl);
                    $("#imageId"+index).html(files[0].uploadId);
                }
            }),
            error: (function (res) {
                alert("失败");
            })
        });
    }

}

function promptInfo(content) {
    $.alert({
        title: '温馨提示',
        content: content,
        buttons: {
            confirm: {
                text: '确认',
                btnClass: 'btn-primary'
            }
        }
    });
}